
local rects = {
    ac.rect.j_rect('item1'),
    ac.rect.j_rect('item2'),
    ac.rect.j_rect('item3'),
    ac.rect.j_rect('item4'),
}

--刷新漂流道具
local area = 2000
local fish_count = 0
function fish_move(unit,idx1)
    local idx1 = idx1 or math.random(1,4)
    local idx2 = idx1%4+1
    local point1 = rects[idx1]:get_point()
    local point2 = rects[idx2]:get_point()
    local angle = point1 / point2
    local range = point1 * point2

    local point3 = rects[idx1]:random()
    if type(unit)=='string' then
        unit = ac.player[16]:create_unit(unit,point3,angle)
        unit:add_restriction '无敌'
        unit:add_restriction '幽灵'
        unit:add_type('鱼')
        fish_count = fish_count + 1
        japi.SetUnitPressUIVisible(unit.handle,false)
        unit:event '单位-死亡'(function()
            fish_count = fish_count - 1
        end)
    else
        point3 = unit:get_point()
    end
    local point4 = point3 - {angle,range}
    unit:set_facing(angle)
    unit:loop(30,function(t)
        point3 = point3 - {angle,unit:get('移动速度')*0.03}
        if point3*point4<=50 then
            t:remove()
            if unit.name=='天赋宝箱' then
                unit:remove()
                return
            end
            fish_move(unit,idx2)
        else
            if not unit:has_restriction '硬直' and unit:is_alive() then
                unit:set_point(point3)
            end
        end
    end)
    
    return unit
end



--添加天赋盒子
local has_pz = {
    ['SR'] = true,
    ['SSR'] = true,
}
local old_func = ac.unit.__index.in_worldscreen
function ac.unit.__index:in_worldscreen()
    if self.name=='天赋盒子' then
        return true
    end
    return old_func(self)
end

function add_tfbox()
    local time = math.random(10,20)
    ac.wait(time*1000,function()
        add_tfbox()
    end)
    ac.each_player(function(player)
        local hero = player.hero
        if hero then
            local list = {}
            local pool = get_allow_card(player)
            for i,name in ipairs(pool) do
                local data = get_card_data(name)
                local class = data.card_class1
                local pz = data['品质']
                if has_pz[pz] and class['通用']==nil then
                    table.insert(list,data)
                end
            end
            if #list>0 then
                local card = table.random(list)
                local unit = fish_move('天赋宝箱')
                unit.card = card
                box_add_art(unit,card)
            end
        end
    end)
end


local weights = {
    -- {'普通漂流箱',50},
    -- {'高级漂流箱',20},
    -- {'珍藏宝箱',5},
    {'一条小鱼',200},
    {'大鱼',40},
    {'超级大鱼',8},
    {'深海巨鲸',2},
    {'泰坦鱼',1},
}
ac.game:event '游戏-设置难度'(function(_,hard)

    ac.loop(5000,function()
        for a=1,20 do
            if fish_count<200 then
                local name = table.get_by_weight(weights)
                fish_move(name)
            end
        end
    end)

    add_tfbox()
end)